package com.teacher.pc.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.teacher.common.CommonResult;
import com.teacher.pojo.entity.Department;
import com.teacher.pojo.vo.department.DepartmentAddVo;
import com.teacher.pojo.vo.department.DepartmentUpdateVo;
import com.teacher.service.IDepartmentService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zfm
 * @since 2021-06-16
 */
@RestController
@RequestMapping("/pc/department")
public class DepartmentController {

    @Autowired
    private IDepartmentService iDepartmentService;

    /**
     * 获取所有部门
     *
     * @return
     */
    @GetMapping("/all")
    @ApiOperation("获取所有部门")
    public CommonResult getAllDepartment() {
        return CommonResult.success(iDepartmentService.list(new QueryWrapper<Department>().lambda()
                .orderByDesc(Department::getSort)));
    }

    /**
     * add department
     *
     * @param departmentAddVo department's info
     * @return add weather department success
     */
    @PostMapping
    @ApiOperation("添加部门")
    @PreAuthorize("hasAuthority('user_department_add')")
    public CommonResult addDepartment(@RequestBody DepartmentAddVo departmentAddVo) {
        return iDepartmentService.addDepartment(departmentAddVo);
    }

    /**
     * delete department
     *
     * @param ids list of department's ids
     * @return weather delete success
     */
    @DeleteMapping
    @ApiOperation("删除部门")
    @PreAuthorize("hasAuthority('user_department_delete')")
    public CommonResult deleteDepartment(@RequestBody List<Long> ids) {
        iDepartmentService.removeByIds(ids);
        return CommonResult.success("删除成功");
    }


    /**
     * update department
     *
     * @param departmentUpdateVo object of department's info
     * @return weather update success
     */
    @PutMapping
    @ApiOperation("更新部门")
    @PreAuthorize("hasAuthority('user_department_update')")
    public CommonResult updateDepartment(@RequestBody DepartmentUpdateVo departmentUpdateVo) {
        Department department = new Department();
        BeanUtils.copyProperties(departmentUpdateVo, department);
        if (iDepartmentService.updateById(department)) {
            return CommonResult.success("更新成功");
        }
        return CommonResult.failed("更新失败");
    }

    /**
     * get department by shape of tree
     *
     * @return department's info of shape of tree
     */
    @GetMapping
    @ApiOperation("树结构查询所有部门")
    @PreAuthorize("hasAuthority('user_department_get')")
    public CommonResult getByTree() {
        return iDepartmentService.getByTree();
    }


    /**
     * 按照权限查询部门
     *
     * @return
     */
    @GetMapping("/byPermission")
    @ApiOperation("按照权限查询部门")
    public CommonResult<List<Department>> getByPermission() {
        return iDepartmentService.getByPermission();
    }

}

